﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS.Modules.RFQ.Controls
{
    public partial class RFQListControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        public void LoadRFQ(int companyid)
        {
            LoadIndustryType(companyid);
        }

        private bool LoadIndustryType(int companyid)
        {
            bool bHasValue = false;

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            String sqlSELECT = "exec dbo.rs_uspGetIndustryType @companyid";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvIndustryType.DataSource = ds;
            gvIndustryType.DataBind();
            myConnection.Close();

            if (gvIndustryType.Rows.Count > 0)
            {
                bHasValue = true;
            }

            return bHasValue;
        }

        private DataSet GetChildListDataSet(int companyid, int industrytype)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            RSUserIdentity userIdentity = GetRSUserIdentity();
            String sqlSELECT = "exec rs_uspGetRFQ @companyid, @industrytype";
 
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@industrytype";
            param.Value = industrytype;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            return ds;
        }
        protected void gvIndustryType_OnRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gv = (GridView)e.Row.FindControl("gvRequest");
                if (null != gv && gv is GridView)
                {
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    int industrytype = Convert.ToInt32(drv["IndustryType"]);
                    gv.DataSource = GetChildListDataSet(companyid, industrytype);
                    gv.DataBind();
                }
            }
        }
        protected void gvRequest_Edit(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int rfqid = Convert.ToInt32(e.CommandArgument.ToString());
            if (rfqid > 0)
            {
                ucModalRFQControl.ShowModal(userIdentity.RSCompanyId, rfqid);
            }
        }

        protected void gvRequest_Remove(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int rfqid = Convert.ToInt32(e.CommandArgument.ToString());
            if (rfqid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@RFQID", SqlDbType.Int, 0, ParameterDirection.Input, rfqid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblRFQ WHERE RFQID=@RFQID AND CompanyID=@CompanyID");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                LoadRFQ(userIdentity.RSCompanyId);
            }
        }

        protected void btnAddRFQ_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            ucModalRFQControl.ShowModal(userIdentity.RSCompanyId, 0);
        }
    }
}